package org.imixs.manik.webstat.jpa;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * A configuration entity to analyse access entities
 * 
 * TimeFrame defines the time frame to analyze. This can be a string with a
 * number of days from now back to the past, or a start and enddate string. So
 * valid values are:
 * <p>
 * "1" = Today "7" = last 7 days <br>
 * "30" = last 30 days<br>
 * "01-05-2011:15:00:00 - 02-05-2011:16:00:30" = a frame of 1 day, 1 hour and 30
 * seconds from now back to the past
 * 
 * @author rsoika
 * 
 */
@javax.persistence.Entity
public class Analyzer implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private AccessLog accessLog;

	private String name;

	private String filter;
	private String exclude;
	private String domains;
	private String timeFrame;
	private Date timeFrom;
	private Date timeTo;

	/**
	 * default constructor for JPA
	 */
	public Analyzer() {
		super();
	}

	/**
	 * returns the unique identifier for the Entity.
	 * 
	 * @return universal id
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public String getId() {
		return id;
	}

	protected void setId(String aID) {
		id = aID;
	}

	// we do not use : cascade = CascadeType.PERSIST
	// because of unidirectional relationship
	@ManyToOne(fetch = FetchType.LAZY)
	public AccessLog getAccessLog() {
		return accessLog;
	}

	public void setAccessLog(AccessLog accessLog) {
		this.accessLog = accessLog;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * defines a reg expression to filter access objects
	 * 
	 * @return
	 */
	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}

	/**
	 * defines a reg expression to exclude access objects
	 * 
	 * @return
	 */
	public String getExclude() {
		return exclude;
	}

	public void setExclude(String exclude) {
		this.exclude = exclude;
	}

	public String getDomains() {
		return domains;
	}

	public void setDomains(String domain) {
		this.domains = domain;
	}

	//@Transient
	@Temporal(TemporalType.TIMESTAMP)
	public Date getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(Date timeFrom) {
		this.timeFrom = timeFrom;
	}

	//@Transient
	@Temporal(TemporalType.TIMESTAMP)
	public Date getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(Date timeTo) {
		this.timeTo = timeTo;
	}

	/**
	 * Time Frame in days or a date time string
	 * 
	 * @return
	 */
	public String getTimeFrame() {
		return timeFrame;
	}

	/**
	 * computes the timefrom and timeTo values
	 * 
	 * @param timeFrame
	 */
	public void setTimeFrame(String atimeFrame) {
		this.timeFrame = atimeFrame;

		Calendar calFrom = Calendar.getInstance();
		Calendar calTo = Calendar.getInstance();
		// set time
		calFrom.set(Calendar.HOUR_OF_DAY, 0);
		calFrom.set(Calendar.MINUTE, 0);
		calFrom.set(Calendar.SECOND, 0);
		calTo.set(Calendar.HOUR_OF_DAY, 23);
		calTo.set(Calendar.MINUTE, 59);
		calTo.set(Calendar.SECOND, 59);

		// try to parse timeframe as a number
		int iTimeFrame = 0;
		try {
			iTimeFrame = Integer.parseInt(timeFrame);
		} catch (NumberFormatException ne) {
			// could not parse number....
		}

		// custom time frame?
		if (iTimeFrame <= 0)
			return;

		if (iTimeFrame > 1) {
			// adjust days ....
			calFrom.add(Calendar.DAY_OF_MONTH, -iTimeFrame);
		}
		// update date time values
		setTimeFrom(calFrom.getTime());
		setTimeTo(calTo.getTime());

	}

}
